package com.fm.demo.jpush

import android.os.Bundle
import android.text.InputType
import android.view.View
import android.widget.TextView
import cn.jpush.android.api.JPushInterface
import com.afollestad.materialdialogs.MaterialDialog
import com.afollestad.materialdialogs.input.input
import com.blankj.utilcode.util.AppUtils
import com.blankj.utilcode.util.RegexUtils
import com.fm.common.base.BaseBActivity
import com.fm.demo.R
import com.fm.demo.bean.TagAliasBean
import com.fm.demo.databinding.ActivityJpushBinding

/**
 * @CreateData: 2019/11/4 14:19
 * @E-Mail: 709674174@qq.com
 * @Author: lyy
 * @Description: 极光推送，测试
 */
class JPushActivity : BaseBActivity<ActivityJpushBinding>(), View.OnClickListener {


    override fun initView(savedInstanceState: Bundle?) {
        initToolbar(binding.rootToolbar,"极光测试", true)
        binding.jpushBtnInit.setOnClickListener(this)
        binding.jpushBtnSetAlias.setOnClickListener(this)
        binding.jpushBtnAddTag.setOnClickListener(this)
        binding.jpushBtnSetPhone.setOnClickListener(this)
        binding.jpushBtnSetTag.setOnClickListener(this)
    }

    override fun onClick(v: View) {
        when (v.id) {
            R.id.jpush_btn_init -> {
                //极光推送初始化
                JPushInterface.setDebugMode(AppUtils.isAppDebug())
                JPushInterface.init(applicationContext)
            }
            R.id.jpush_btn_set_alias -> {
                MaterialDialog(mContext).show {
                    title(text = "请输入别名")
                    view.findViewById<TextView>(R.id.md_text_title).textSize = 16f
                    input { _, charSequence ->
                        val inputString = charSequence.toString()
                        if (inputString.isNotEmpty()) {
                            TagAliasOperatorHelper.instance.handleAction(
                                mContext, 101, TagAliasBean(
                                    action = TagAliasOperatorHelper.ACTION_SET,
                                    isAliasAction = true,
                                    alias = inputString
                                )
                            )
                        }
                    }
                }
            }
            R.id.jpush_btn_set_tag -> {
                MaterialDialog(mContext).show {
                    title(text = "请输入标签,多个标签请使用','分割")
                    view.findViewById<TextView>(R.id.md_text_title).textSize = 16f
                    input { _, charSequence ->
                        val inputString = charSequence.toString()
                        if (inputString.isNotEmpty()) {
                            val hashSet = HashSet<String>()
                            val tags = inputString.split(",".toRegex()).dropLastWhile { it.isEmpty() }
                            tags.forEach {
                                hashSet.add(it)
                            }
                            TagAliasOperatorHelper.instance.handleAction(
                                mContext, 102, TagAliasBean(
                                    action = TagAliasOperatorHelper.ACTION_SET,
                                    tags = hashSet
                                )
                            )
                        }
                    }
                }
            }
            R.id.jpush_btn_add_tag -> {
                MaterialDialog(mContext).show {
                    title(text = "请输入标签,多个标签请使用','分割")
                    view.findViewById<TextView>(R.id.md_text_title).textSize = 16f
                    input { _, charSequence ->
                        val inputString = charSequence.toString()
                        if (inputString.isNotEmpty()) {
                            val hashSet = HashSet<String>()
                            val tags = inputString.split(",".toRegex()).dropLastWhile { it.isEmpty() }
                            tags.forEach {
                                hashSet.add(it)
                            }
                            TagAliasOperatorHelper.instance.handleAction(
                                mContext, 103, TagAliasBean(
                                    action = TagAliasOperatorHelper.ACTION_ADD,
                                    tags = hashSet
                                )
                            )
                        }
                    }
                }
            }
            R.id.jpush_btn_set_phone -> {
                MaterialDialog(mContext).show {
                    title(text = "请输入手机号码")
                    view.findViewById<TextView>(R.id.md_text_title).textSize = 16f
                    input(
                        maxLength = 11,
                        inputType = InputType.TYPE_CLASS_NUMBER
                    ) { _, charSequence ->
                        if (charSequence.isNotEmpty() && RegexUtils.isMobileSimple(charSequence)) {
                            JPushInterface.setMobileNumber(mContext, 104, charSequence.toString())
                        }
                    }
                }
            }
        }
    }
}
